package cn.z2huo.knowledge.concurrency.lock.reentrantlock.interrupt;

import cn.hutool.core.thread.ThreadUtil;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 *
 * @author z2huo
 */
class ReentrantLockInterruptiblyTest {

    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        IncrementData incrementData = new IncrementData(lock);

        Runnable runnable = () -> {
            incrementData.lockInterruptiblyAndFastIncrease(Thread.currentThread().getName());
        };

        Thread t1 = new Thread(runnable, "thread-1");
        Thread t2 = new Thread(runnable, "thread-2");

        t1.start();
        t2.start();

        ThreadUtil.sleep(100);

        t1.interrupt();
        t2.interrupt();

        ThreadUtil.sleep(5, TimeUnit.SECONDS);
        System.out.println("result is " + incrementData.getSum());
    }

}
